;(function($){
	window.hbServerLink = {
		config: {
			serverUrl: '',
			joggle:{
				dataUrl: '',
				saveUserUrl: '',
				lotteryUrl: '',
			},
			hbConfig: {},
			onTryHB: null,
			onStopHB: null,
		},
		data: {
			inited: false,
			actioning: false,
			nodeBeginTime: 0,
			
		},
		_init: function(params) {
			var self = this;
			
			if (self.inited) {
				return;
			}
			//合并配置
			self.config = $.extend(true, self.config, params);
			var hbd = self.config.hbConfig,
				d = self.data;
			d.nodeBeginTime = hbd.beginTime;
			
			self.inited = true;
		},
		isGiftTime: function() {
			var self = this,
				d = self.data,
				hbd = self.config.hbConfig;

			//活动未开始或已结束
			if(hbd.baseTime < hbd.beginTime || hbd.baseTime > hbd.endTime) {
				return false;
			}

			//节点未开始或已结束
			if(hbd.baseTime < d.nodeBeginTime || hbd.baseTime > d.nodeBeginTime + hbd.duration) {
				return false;
			}

			return true;
		},
		tryAction: function() {
			var self = this,
				c = self.config,
				d = self.data,
				hbd = c.hbConfig;
			
			//参加过的就不触发了
			if (hbd.myGift > 0) {
				return;
			}
			
			//是否在活动时间
			if(self.isGiftTime()) {
				if (d.actioning) return;
				
				//触发红包开始
				if (typeof c.onTryHB == 'function') {
					c.onTryHB();
				} else {
					console.log('start hb');
				}

				//到持续时间后停止
				setTimeout(function() {
					if (typeof c.onStopHB == 'function') {
						c.onStopHB();
					} else {
						console.log('start stop');
					}
				}, hbd.duration);
				
				d.actioning = true;		
			} else {
				d.actioning = false;
			}
		},
		//检测是否已获取红包礼物
		popHaveGift: function() {
			var self = this,
				hbd = self.config.hbConfig;
			
			if(hbd.myGift > 0 && hbd.name == "") {
				var data = {
					entity_id: hbd.myGift,
					id: hbd.myGift,
				
				}
				pophb(data);
				
				return true;
			}
			
			return false;
		},
		//运行
		initRun: function(params) {
			var self = this;
			
			self._init(params);
			var	d = self.data,
				hbd = self.config.hbConfig;
			if (!self.popHaveGift()) {
				function timetry() {
					if (hbd.baseTime > hbd.endTime) {
						console.log('hb active end');
						return;
					}
					//开启
					var t = setTimeout(function(){
						clearTimeout(t);
						hbd.baseTime += 1000;
						//节点是否已结束，结束则查找下一个可用节点 + hbd.duration
						while (d.nodeBeginTime < hbd.baseTime){
							d.nodeBeginTime += hbd.intervalTime  + hbd.duration;
						}
						//尝试触发活动
						self.tryAction();
						
						timetry();
					}, 1000);
				}
				
				timetry();
			}
		},
		//设置触发红包
		tryHB: function(callback) {
			if (typeof callback == 'function') {
				this.config.onTryHB = callback;
			}
		},
		//设置停止红包
		stopHB: function(callback) {
			if (typeof callback == 'function') {
				this.config.onStopHB = callback;
			}			
		},
		//抽奖
		lottery: function(callback) {
			var self = this,
				c = self.config,
				hbd = c.hbConfig,
				lotteryUrl = c.serverUrl + c.joggle.lotteryUrl;
				
			if (typeof c.onStopHB == 'function') {
				self.config.onStopHB();
			}

			$.ajax({
				type:"get",
				url: lotteryUrl,
				async:true,
				dataType: 'json',
				success: function(result) {
					console.log(result);
					if (result.code > 0 && result.gift != undefined) {
						//设置已领
						hbd.myGift = result.gift.entity_id;
					}
					
					callback(result);
				}
			});
		},
		submitUser: function(subdata, callback) {
			var self = this,
				c = self.config,
				hbd = c.hbConfig;
			
			if (!(hbd.myGift > 0)) {
				return;
			}
			
			var self = this,
				c = self.config,
				hbd = c.hbConfig,
				suUrl = c.serverUrl + c.joggle.saveUserUrl;
			console.log(suUrl);
			$.ajax({
				type:"post",
				url: suUrl,
				async:true,
				data: subdata, 
				success: function(result) {
					if (typeof callback == 'function') {
						callback(result);
					}
				},
				error: function() {
					alert('提交用户数据出错');
				}
			});
		}
	}
})(jQuery);
